package com.examples.springboot.jpa.gui.climbers;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.examples.springboot.jpa.pojos.ClimberDto;
import com.examples.springboot.jpa.pojos.ClimberPhoneNumberVo;
import com.examples.springboot.jpa.pojos.ClimberVo;
import com.examples.springboot.jpa.pojos.MountVo;

/**
 * A custom table model for the table of ClimberDto. 
 *
 */
@SuppressWarnings("serial")
public class ClimberTableModel extends AbstractTableModel {
	private String[] columnNames = {"No.", "Name", "Mounts", "PhoneNumbers"};
	private final List<ClimberVo> listClimber = new ArrayList<>();

//	public ClimberTableModel() {
//  }
	public ClimberTableModel(List<ClimberDto> listClimber) {
	  setData(listClimber);
	}

	
	public void setData(List<ClimberDto> listClimber){
	  for (ClimberDto climberDto : listClimber) {
      this.listClimber.add(new ClimberVo(climberDto));
    }
	  System.out.println("size = "+this.listClimber.size());
	  if(!listClimber.isEmpty())
	  {
	    fireTableDataChanged();
	  }
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}
	
  public String getColumnName(int column) {
    return columnNames[column];
  }

  public Class<?> getColumnClass(int column) {
    return getValueAt(0, column).getClass();
  }

	@Override
	public int getRowCount() {
		return listClimber.size();
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
	  ClimberVo climber = listClimber.get(rowIndex);

		switch (columnIndex) {
		case 1:
      climber.setName((String) value);
			break;
		case 2:
		  climber.setMount((MountVo)value);
			break;
		case 3:
      climber.setPhoneNumber((ClimberPhoneNumberVo) value);
			break;
		}		
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object returnValue = null;
		ClimberVo climber = listClimber.get(rowIndex);
		
		switch (columnIndex) {
		case 0:
				returnValue = climber.getId();
			break;
		case 1:
				returnValue = climber.getName();
			break;
		case 2:
				returnValue = climber.getMount();
			break;
		case 3:
				returnValue = climber.getPhoneNumber();
			break;
		}
		
		return returnValue;
	}

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex > 0;
    }	
}
